<template>
	<view class="content">
		<u-picker :show="showControl" keyName="name" :columns="controlList" closeOnClickOverlay @cancel="showControl = false"
			@close="showControl = false" @confirm="_setControl"></u-picker>
		<u-picker :show="showType" keyName="dict_label" :columns="typeList" closeOnClickOverlay @cancel="showType = false"
			@close="showType = false" @confirm="_setCurType"></u-picker>
		<u-datetime-picker :show="showStart" v-model="startStamp" closeOnClickOverlay mode="datetime"
			@confirm="confirmStart" @cancel="showStart = false" @close="showStart = false"></u-datetime-picker>
		<view class="section-5">
			<view class="section-bdoy"></view>
		</view>
	
		<view class="section-0">
			<view class="wrapper">
				<view class="label">作业类型</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">作业部位</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">作业票名称</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">地址位置</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">管控措施</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">流程状态</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">导入时间</view>
				<view class="value">
					<view class="date">
						<view class="input-body">{{start}}</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="section-0">
			<view class="wrapper">
				<view class="label">计划开始时间</view>
				<view class="value">
					<view class="date" @click="showStart = true">
						<view class="input-body">{{start}}</view>
						<view class="iconfont icon-rili"></view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">计划完成时间</view>
				<view class="value">
					<view class="date" @click="showStart = true">
						<view class="input-body">{{start}}</view>
						<view class="iconfont icon-rili"></view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">实际开始时间</view>
				<view class="value">
					<view class="date" @click="showStart = true">
						<view class="input-body">{{start}}</view>
						<view class="iconfont icon-rili"></view>
					</view>
				</view>
			</view>
		</view>
		<view class="section-0">
			<view class="wrapper">
				<view class="label">实际完成时间</view>
				<view class="value">
					<view class="date" @click="showStart = true">
						<view class="input-body">{{start}}</view>
						<view class="iconfont icon-rili"></view>
					</view>
				</view>
			</view>
		</view>

		<view class="footer-space"></view>
		<view class="footer">
			<!-- <view class="record" @click="go()">
				<view class="iconfont icon-lishijilu_huaban"></view>
				<text>历史记录</text>
			</view> -->
			<view class="submit" @click="submit">
				提交
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	import { addMeeting } from '@/api/meeting.js';
	import { uploadUrl } from '@/baseUrl.js';
	import { typeSearchApi } from '@/api/statistics.js';
	var recordObj = '';
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = true;
	export default {
		data() {
			return {
				timecount: '00:00:00',
				hour: 0,
				minute: 0,
				second: 0,
				timer1:'',
				isZant: false,
				gotime:0,
				typeList: [
					 [
						{
							"dict_code": 283,
							"dict_label": "例会",
							"dict_value": "1"
						},
						{
							"dict_code": 284,
							"dict_label": "站班会",
							"dict_value": "2"
						},
						{
							"dict_code": 285,
							"dict_label": "安委会",
							"dict_value": "3"
						},
						{
							"dict_code": 286,
							"dict_label": "启委会",
							"dict_value": "5"
						},
						{
							"dict_code": 287,
							"dict_label": "协调会",
							"dict_value": "6"
						},
						{
							"dict_code": 412,
							"dict_label": "第一次工地例会",
							"dict_value": "7"
						}
					]
				],
				curType: '例会',
				memo: '',
				name: '',
				showType: false,
				start: '',
				startStamp: Number(new Date()),
				showStart: false,
				fileList: [],
				fileVideoList: [],
				form: {
					meeting_name: '',
					meeting_type: '',
					meeting_time: '',
					meet_file_content: '',
					meeting_file_id: '',
					meeting_av_id: '',
					meeting_audio_id: '',
					project_manage_id: '',
					meeting_duration: ''
				},
				recordStatus: 'off',
				loading: false,
				timer: null,
				showControl: false,
				project_manage_id: '',
				project_manage_name: '',
				controlList: [],
				meeting_duration: ''
			}
		},
		computed: {
			...mapGetters({
				curProject: 'system/getCurProject',
				userInfo: 'system/getUserInfo'
			})
		},
		onLoad() {
			recordObj = plus.audio.getRecorder();
			
		},
		onUnload() {
			if(this.timer1) {  
				clearTimeout(this.timer1);  
				this.timer1 = '';  
			}  
		},
		onReady() {
			this.typeSearch();
		},
		beforeDestroy() {
			clearInterval(this.timer);
			this.timer = null;
		},

		methods: {
			// 点击开始录音就开始计时
			getTimeInterval(){
				clearInterval(this.timer1);
				this.timer1 = setInterval(()=> {
					this.second += 1;
					if(this.second >= 60){
						this.minute += 1;
						this.second = 0;
					}
					if(this.minute >= 60 && this.second >= 60){
						this.minute += 0;
						this.hour += 1;
					}
					this.timecount = this.showNum(this.hour)+":"+this.showNum(this.minute)+":"+this.showNum(this.second);
					console.log("this.timecount",this.timecount)
				},1000);
				
			},
			showNum(num) {
				if (num < 10) {
					return '0' + num
				}
				return num
			},
			
			
			go() {
				const params = {
					type: 'history',
					user_name: this.userInfo.nick_name,
				}
				uni.navigateTo({
					url: '/pages/statistics/comprehensive/duty/meeting/list/index?params=' + JSON.stringify(params)
				})
			},
			handleRecord() {
				// this.timecount = '00:00:00';
				// this.hour = 0;
				// this.minute = 0;
				// this.second = 0;
				
				if (this.recordStatus === 'fin') {
					return;
				} else if (this.recordStatus === 'off') {
					this.recordStatus = 'on';
					this.getTimeInterval()
					this.startRecord();
				} else if (this.recordStatus === 'on') {
					this.recordStatus = 'fin';
					this.gotime == 1
					this.endRecord();
				}
			},
			startRecord() {
				let _this = this;
				recordObj.record({filename:"_doc/audio/"}, async (url) => {
					_this.loading = true;
					uni.showLoading({title: '上传中...'});
					const result = await this.uploadFilePromise(url)
					_this.meeting_audio_id = result.data.file_id;
					console.log(_this.meeting_audio_id, '_this.meeting_audio_id')
					uni.hideLoading();
					_this.loading = false;
				}, (e) => {
					console.log(e, 'error');
					uni.showToast(
						{title: '请检查权限', duration: 1000, icon: 'none' }
					);
				});
			},
			endRecord() {
				recordObj.stop();
			},
			async submit() {
				if (this.recordStatus === 'on') {
					this.loading = true;
					this.endRecord();
					this.recordStatus = 'fin';
				}
				let obj = {}
				obj.project_id = this.curProject.id
				obj.meeting_name = this.name
				obj.meeting_type = this.curValue
				obj.meeting_time = this.start
				obj.meet_file_content = this.memo
				obj.meeting_file_id = this.fileList[0] ? this.fileList[0].file_id : ''
				obj.meeting_av_id = this.fileVideoList[0] ? this.fileVideoList[0].file_id : ''
				obj.project_manage_id =  this.project_manage_id
				obj.user_name = this.userInfo.nick_name
				obj.meeting_duration = this.meeting_duration
				let limit = 25;
				this.timer = setInterval(() => {
					limit--;
					if (!this.loading || limit <= 0) {
						obj.meeting_audio_id = this.meeting_audio_id
						this.handleSubmit(obj);
						clearInterval(this.timer);
						this.timer = null;
					}
				}, 200);
			},
			async handleSubmit(obj) {
				uni.showLoading({title: '提交中...'});
				const { data } = await addMeeting(obj);
				uni.hideLoading();
				if (data.code == 1) {
					uni.showToast(
						{title: '提交成功', duration: 1000, icon: 'success' }
					);
					uni.navigateBack({
						delta: 1
					})
				}
			},
			async typeSearch(obj) {
				uni.showLoading({title: '加载中...'});
				const { data } = await typeSearchApi({type: '会议', project_id: this.curProject.id});
				uni.hideLoading();
				if (data.code == 1) {
					let arr = data.data.map(item => ({...item, name: item.name.substring(3, item.name.length)}))
					this.controlList = [arr];
				}
			},
			// 删除
			deleteMedia(event, type) {
				this[type].splice(event.index, 1)
			},
			// 新增
			async afterRead(event, type) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[type].length
				lists.map((item) => {
					this[type].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[type][fileListLen]
					this[type].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						file_id: result.data.file_id
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: uploadUrl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						success: (res) => {
							resolve(JSON.parse(res.data))
						}
					});
				})
			},
			_setCurType(e) {
				this.curType = e.value[0].dict_label;
				this.curValue = e.value[0].dict_code;
				this.showType = false;
			},
			_setControl(e) {
				this.project_manage_id = e.value[0].id;
				this.project_manage_name = e.value[0].name;
				this.showControl = false;
			},
			formatDate(timeStamp) {
				const date = new Date(timeStamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				const Y = date.getFullYear() + '-';
				const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				const D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
				const h = (date.getHours() < 10 ? "0" + date.getHours() :  date.getHours()) + ':';
				const m = (date.getMinutes() < 10 ? "0" + date.getMinutes() :  date.getMinutes()) + ':';
				const s = (date.getSeconds() < 10 ? "0" + date.getSeconds() :  date.getSeconds());
				return Y + M + D + h + m + s;
			},
			confirmStart(val) {
				const timeStamp = val.value;
				this.start = this.formatDate(timeStamp);
				this.showStart = false;
				this.form.meeting_time = this.start;
			},
		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .uni-picker-view-content>* {
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		padding: 0 10rpx;
		width: calc(100% - 20rpx);
	}
	.section-5{
		width: 100%;
		background-color: #2c5cff;
		.section-bdoy{
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			background-color: #fff;
			overflow: hidden;
			width: 100%;
			height: 50rpx;
		}
	}
	.footer-space {
		height: 150rpx;
	}
	.footer {
		width: 100%;
		height: 150rpx;
		background-color: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		justify-content: space-around;
		align-items: center;
		box-shadow: 0 0 20rpx 10rpx rgba(#bcbcb5, 0.17);

		.record {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;

			.icon-lishijilu_huaban {
				font-size: 43rpx;
				color: #3b3c40;
			}

			text {
				font-size: 26rpx;
				color: #3b3c40;
				margin-top: 10rpx;
			}
		}

		.submit {
			width: 287rpx;
			height: 83rpx;
			background-color: #1534ce;
			border-radius: 20rpx;
			letter-spacing: 14rpx;
			color: #ffffff;
			font-size: 26rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	.section-1 {
		width: 670rpx;
		margin-left: 40rpx;
		height: 100rpx;
		background-color: #0535d6;
		box-shadow: 0rpx 13rpx 35rpx 0rpx rgba(51, 91, 222, 0.42);
		border-radius: 35rpx;
		margin-bottom: 90rpx;
		overflow: hidden;
		line-height: 100rpx;

		.label {
			float: left;
			font-size: 26rpx;
			font-weight: bold;
			font-stretch: normal;
			line-height: 100rpx;
			letter-spacing: 0rpx;
			margin-left: 40rpx;
			color: #ffffff;
		}
		.name{
			overflow: hidden;
			float: left;
			width: 300rpx;
			line-height: 100rpx;
			font-size: 26rpx;
			color: #ffffff;
			margin-left: 24rpx;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.iconfont {
			margin-right: 70rpx;
			font-size: 42rpx;
			color: #fff;
			float: right;
		}
	}

	.content {
		.wrapper {
			width: 670rpx;
			margin-left: 40rpx;
			border-radius: 35rpx;
			background-size: cover;
			background-repeat: no-repeat;
			overflow: hidden;
			box-sizing: border-box;
			box-shadow: 0 0 20rpx 10rpx rgba(#bcbcb5, 0.17);
			padding: 28rpx 40rpx;

			.label {
				width: 160rpx;
				float: left;
				font-size: 26rpx;
				font-weight: bold;
				font-stretch: normal;
				height: 48rpx;
				line-height: 48rpx;
				letter-spacing: 0px;
				color: #46464a;
			}

			.title {
				width: 100%;
				height: 30rpx;
				line-height: 30rpx;
				border-bottom: solid 3rpx #f2f2f0;
				font-size: 26rpx;
				font-weight: bold;
				font-stretch: normal;
				letter-spacing: 0rpx;
				color: #46464a;
				padding-bottom: 28rpx;

				.iconfont {
					float: right;
					font-size: 30rpx;
					color: #46464a;
				}
			}

			.value {
				float: left;
				width: 420rpx;
				margin-left: 10rpx;
				.input-2{
					width: 100%;
					height: 52rpx;
					border-radius: 20rpx;
					border: solid 2rpx #0535d6;
					line-height: 52rpx;
					position: relative;
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.7);
					box-sizing: border-box;
					// padding-left: 20rpx;
					text-align: center;
					color: #0535d6;
					padding-right: 30rpx;
					.input-body{
						box-sizing: border-box;
						width: 100%;
						height: 44rpx;
						padding: 0!important;
						border: none;
						padding-left: 30rpx!important;
						padding-right: 30rpx!important;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0,0,0,0.7);
					}
					.input-placeholder{
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0,0,0,0.5);
					}
				}	
				.audio {
					width: 50rpx;
					height: 50rpx;
					background-color: #ffffff;
					border-radius: 50%;
					box-shadow: 0 0 20rpx 10rpx rgba(#bcbcb5, 0.17);
					overflow: hidden;

					float: left;
					.audio-center {
						margin-top: 13rpx;
						margin-left: 13rpx;
						border-radius: 50%;
						width: 24rpx;
						height: 24rpx;
						background-color: #ed451b;
					}
					.audio-center__on{
						animation: scale 2s infinite;
					}
					.audio-center__fin{
						background-color: rgba(0, 0, 0, 0.5);;
					}
					@keyframes scale{
						0%{
							transform: scale(1);
							opacity: 1;
						}
						  50%{
							transform: scale(1.5);
							opacity: 0.5;
						  }
						  100%{
							transform: scale(1);
							opacity: 1;
						  }
					}
				}
				.fin-tips{
					height: 50rpx;
					line-height: 50rpx;
					font-size: 22rpx;
					color: rgba(0, 0, 0, 0.5);
					float: left;
					margin-left: 20rpx;
				}
				.time{
					position: absolute;
					right: 100rpx;
				}
				.upload-wrap {
					width: 100%;
					min-height: 48rpx;
					border-radius: 20rpx;
					border: solid 2rpx #0535d6;
					position: relative;
					.file_name{
						padding: 9rpx 0;
						line-height: 30rpx;
						color: #1534ce;
						font-size: 26rpx;
						word-break: break-all;
					}
					.iconfont {
						position: absolute;
						right: 25rpx;
						line-height: 48rpx;
						color: #1534ce;
						font-size: 28rpx;
						top: 2rpx;
					}
				}

				.date {
					width: 100%;
					height: 52rpx;
					border-radius: 20rpx;
					border: solid 2rpx #0535d6;
					line-height: 48rpx;
					position: relative;
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.7);
					box-sizing: border-box;
					// padding-left: 20rpx;
					text-align: center;
					color: #0535d6;
					padding-right: 50rpx;

					.iconfont {
						position: absolute;
						right: 25rpx;
						top: 0rpx;
						font-size: 30rpx;
						color: #0535d6;
					}
				}

				.picker {
					width: 100%;
					height: 48rpx;
					border-radius: 20rpx;
					border: solid 2rpx #0535d6;
					line-height: 44rpx;

					text {
						font-size: 26rpx;
						letter-spacing: 0rpx;
						color: #46464a;
						margin-left: 28rpx;
					}

					.iconfont {
						float: right;
						font-size: 32rpx;
					}
				}

				.input {
					padding-top: 18rpx;

					.input-body {
						box-sizing: border-box;
						width: 100%;
						height: 44rpx;
						padding: 0 !important;
						border: none;
						line-height: 44rpx;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.7);
					}

					.input-placeholder {
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.5);
					}
				}

				.textarea {
					width: 100%;
					height: 200rpx;
					line-height: 30rpx;

					.textarea-body {
						box-sizing: border-box;
						width: 100%;
						height: 200rpx;
						border: none;
						padding: 18rpx 0rpx !important;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;

						/deep/ .u-textarea__field {
							height: auto !important;
							font-size: 25rpx;
						}
					}

					/deep/ .u-textarea--disabled {
						background-color: #fff;
					}

					.textarea-placeholder {
						font-size: 25rpx;
						font-weight: normal;
						font-stretch: normal;
						line-height: 50rpx;
						letter-spacing: 3rpx;
						color: rgba(0, 0, 0, 0.5);
					}
				}
			}

			.l-value {
				width: 100%;
			}
		}

		.section-0 {
			margin-bottom: 24rpx;

		}
	}
</style>
